package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.PatientSportRecordEntity;

public class PatientSportRecordDAO {
	public int insert(Connection conn, PatientSportRecordEntity entity) throws SQLException {
		int id=-1;
		PreparedStatement ps = conn.prepareStatement(
				"insert into patient_sport_record(sport_plan_id,add_time,way,time,status,patient_id) values(?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
		ps.setInt(1, entity.getSport_plan_id());
		ps.setTimestamp(2, entity.getAdd_time());
		ps.setString(3, entity.getWay());
		ps.setInt(4, entity.getTime());
		ps.setInt(5, entity.getStatus());
		ps.setInt(6, entity.getPatient_id());
		if (ps.executeUpdate() > 0) {
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()) {
				id=rs.getInt(1);
			}
		}
		return id;
	}
	
	public boolean update(Connection conn, PatientSportRecordEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"update patient_sport_record set sport_plan_id=?,add_time=?,way=?,time=?,status=?,patient_id=? where id=?");
		ps.setInt(1, entity.getSport_plan_id());
		ps.setTimestamp(2, entity.getAdd_time());
		ps.setString(3, entity.getWay());
		ps.setInt(4, entity.getTime());
		ps.setInt(5, entity.getStatus());
		ps.setInt(6, entity.getPatient_id());
		ps.setInt(7, entity.getId());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}
	
	public PatientSportRecordEntity findById(Connection conn,int id) throws SQLException {
		PatientSportRecordEntity entity=null;
		PreparedStatement ps = conn.prepareStatement("select * from patient_sport_record where id =?");
		ps.setInt(1, id);
		ResultSet rs=ps.executeQuery();
		if(rs.next()) {
			entity=fill(rs);
		}
		return entity;
	}

	private PatientSportRecordEntity fill(ResultSet rs) throws SQLException {
		PatientSportRecordEntity entity=new PatientSportRecordEntity();
		entity.setAdd_time(rs.getTimestamp("add_time"));
		entity.setId(rs.getInt("id"));
		entity.setPatient_id(rs.getInt("patient_id"));
		entity.setSport_plan_id(rs.getInt("sport_plan_id"));
		entity.setStatus(rs.getInt("status"));
		entity.setTime(rs.getInt("time"));
		entity.setWay(rs.getString("way"));
		return entity;
	}

	public List<PatientSportRecordEntity> listSportPatientByPlanId(Connection conn, int sport_plan_id) throws SQLException {
		List<PatientSportRecordEntity> list=new LinkedList<>();
		PreparedStatement ps = conn.prepareStatement("select * from patient_sport_record where sport_plan_id =? and status=1");
		ps.setInt(1, sport_plan_id);
		ResultSet rs=ps.executeQuery();
		while(rs.next()) {
			PatientSportRecordEntity entity=fill(rs);
			list.add(entity);
		}
		return list;
	}

	public List<PatientSportRecordEntity> listPatientRecordsByPlanId(Connection conn, int sport_plan_id,
			int patient_id) throws SQLException {
		List<PatientSportRecordEntity> list=new LinkedList<>();
		PreparedStatement ps = conn.prepareStatement("select * from patient_sport_record where sport_plan_id =? and patient_id=? and status=1");
		ps.setInt(1, sport_plan_id);
		ps.setInt(2, patient_id);
		ResultSet rs=ps.executeQuery();
		while(rs.next()) {
			PatientSportRecordEntity entity=fill(rs);
			list.add(entity);
		}
		return list;
	}
}
